package com.da.javatest.leetCode;

import java.util.Arrays;

/**
 * @author chenlida
 * @date 2018/12/10 16:22
 * @description
 */
public class Code807 {
    public static void main(String[] args) {
        int[][] grid = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3}, {0, 3, 1, 0}};
        System.out.println(new Code807().maxIncreaseKeepingSkyline(grid));
    }

    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int total = 0;
        int[] horizontalMax = getHorizontalMax(grid);
        int[] verticalMax = getVerticalMax(grid);
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                int min = horizontalMax[i] < verticalMax[j] ? horizontalMax[i] : verticalMax[j];
                total += min - grid[i][j];
            }
        }
        return total;
    }

    private int[] getHorizontalMax(int[][] grid) {
        int[] maxArray = new int[grid.length];
        for (int i = 0; i < grid.length; i++) {
            maxArray[i] = Arrays.stream(grid[i]).max().getAsInt();
        }
        return maxArray;
    }

    private int[] getVerticalMax(int[][] grid) {
        int[] maxArray = new int[grid.length];
        for (int i = 0; i < grid[0].length; i++) {
            int[] tempArry = new int[grid[0].length];
            for (int j = 0; j < grid.length; j++) {
                tempArry[j] = grid[j][i];
            }
            maxArray[i] = Arrays.stream(tempArry).max().getAsInt();
        }
        return maxArray;
    }
}
